using System;
using System.Collections.Generic;

namespace Developex.DatabaseWrapper.CSVImportExport
{

	///<summary>
	///</summary>
	public class EICalculatedField : EIField
	{
		/// <summary>
		/// Delegate which needed for event
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		public delegate void GetValueOfCalculatedField(object sender, EICalculatedFieldEventArgs e);

		/// <summary>
		/// Event which called when need to now field value
		/// </summary>
		public event GetValueOfCalculatedField GetValue;

		/// <summary>
		/// Returns calculated Value
		/// </summary>
		/// <returns></returns>
		private string OnGetValue()
		{
			if (GetValue != null && RowObject != null)
			{
				EICalculatedFieldEventArgs e = new EICalculatedFieldEventArgs(rowObject);
				GetValue.Invoke(this, e);
				return e.StringValue;
			}
			return null;
		}

		private Row rowObject;

		/// <summary>
		/// Get/Set Row object before call getter of property Value
		/// </summary>
		public Row RowObject
		{
			get { return rowObject; }
			set { rowObject = value; }
		}

		/// <summary>
		/// Get/Set Value
		/// </summary>
		public override object Value
		{
			get
			{
				return OnGetValue();
			}
			set
			{
				base.Value = value;
			}
		}

		/// <summary>
		/// Default Constructor
		/// </summary>
		/// <param name="name"></param>
		public EICalculatedField(string name)
			: base(name)
		{
			this.Name = name;
			this.DisplayName = name;
		}

		/// <summary>
		/// Default Constructor
		/// </summary>
		/// <param name="name"></param>
		/// <param name="value"></param>
		public EICalculatedField(string name, object value)
			: this(name, value, name)
		{
		}

		/// <summary>
		/// Default Constructor
		/// </summary>
		/// <param name="name"></param>
		/// <param name="value"></param>
		/// <param name="displayName"></param>
		public EICalculatedField(string name, object value, string displayName)
			: this(name, value, displayName, null)
		{
		}

		/// <summary>
		/// Default Constructor
		/// </summary>
		/// <param name="name"></param>
		/// <param name="value"></param>
		/// <param name="displayName"></param>
		/// <param name="position"></param>
		public EICalculatedField(string name, object value, string displayName, int? position)
			: base(name, value, displayName, position)
		{
		}
	}

	/// <summary>
	/// 
	/// </summary>
	public class EICalculatedFieldEventArgs : EventArgs
	{
		/// <summary>
		/// Get/Set Row object
		/// </summary>
		private Row rowObject;
		/// <summary>
		/// Get/Set value of the field in string format
		/// </summary>
		private string stringValue;

		/// <summary>
		/// Get/Set Row object
		/// </summary>
		public Row RowObject
		{
			get { return rowObject; }
			set { rowObject = value; }
		}

		/// <summary>
		/// Get/Set value of the field in string format
		/// </summary>
		public string StringValue
		{
			get { return stringValue; }
			set { stringValue = value; }
		}

		/// <summary>
		/// Default Constructor
		/// </summary>
		/// <param name="rowObject"></param>
		public EICalculatedFieldEventArgs(Row rowObject)
		{
			this.rowObject = rowObject;
		}

		/// <summary>
		/// Default constructor with value init
		/// </summary>
		/// <param name="stringValue"></param>
		/// <param name="rowObject"></param>
		public EICalculatedFieldEventArgs(string stringValue, Row rowObject)
		{
			this.stringValue = stringValue;
			this.rowObject = rowObject;
		}
	}
}